'use strict';

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;

  const ShortConnectionSchema = new Schema({
    /**
     * 标题
     */
    url: { type: String, default: '' },
    /**
     * 时间
     */
    date: { type: Date },
    /**
     * 内容
     */
    short: { type: String, default: '' },
  });

  /**
   * 删除过期的数据
   * @param { Number } overdueTime 过期时间
   */
  ShortConnectionSchema.statics.removeModel = function(overdueTime) {
    const currentDate = new Date().getTime();
    const beforeDate = new Date(currentDate - (overdueTime * 1000));
    return this.deleteMany({
      date: {
        $lt: beforeDate,
      },
    });
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 查询短连接
   * * @param { Number } short 短连接
   */
  ShortConnectionSchema.statics.findWithShort = function(short) {
    return this.findOne({
      short,
    });
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 查询短连接
   * * @param { Number } url 链接
   */
  ShortConnectionSchema.statics.findWithUrl = function(url) {
    return this.findOne({
      url,
    });
  };

  return mongoose.model('ShortConnection', ShortConnectionSchema, 'shortConnection');
};
